import { createStore } from "redux";
//创建state的数据
const initState = {
  user: sessionStorage.getItem("user")
    ? JSON.parse(sessionStorage.getItem("user"))
    : "",
};
//创建全局store的处理函数
function userReducer(state = initState, action) {
  const { type, payload } = action;
  switch (type) {
    case "setState":
      state.user = sessionStorage.getItem("user") || payload;
      //如果传来了用户信息则将其存到sessionStorage里面
      if (payload) {
        sessionStorage.setItem("user", JSON.stringify(payload));
      }
      return { ...state };
    case "delState":
      state.user = "";
      return { ...state };
    default:
      return state;
  }
}
const store = createStore(userReducer);
export default store;
